import { AnyAction } from "redux";
import { SET_CURRENT_CATEGORY } from "../actiontypes";
import * as TYPES from "../actiontypes";
export type currentCategoryType = "all" | "react" | "vue" | "javascript";
export interface Slider {
	url: string;
}
export interface Lesson {
	id: string;
	title: string;
	video: string;
	poster: string;
	url: string;
	price: string;
	category: string;
}

export interface Lessons {
	loading: boolean;
	list: Lesson[];
	hasMore: boolean;
	offset: number;
	limit: number;
}

export interface LessonResult {
	data: Lesson;
	success: boolean;
}

export interface HomeState {
	currentCategory: currentCategoryType;
	sliders: Slider[];
	lessons: Lessons;
}

const homeInitState: HomeState = {
	currentCategory: "all",
	sliders: [],
	lessons: {
		loading: false,
		list: [],
		hasMore: true,
		offset: 0,
		limit: 5,
	},
};

export default function (state: HomeState = homeInitState, action: AnyAction) {
	switch (action.type) {
		case SET_CURRENT_CATEGORY:
			return { ...state, currentCategory: action.payload };
		case TYPES.GET_SLIDERS:
			return { ...state, sliders: action.payload.data };
		case TYPES.SET_LESSONS_LOADING:
			state.lessons.loading = action.payload;
			return state;
		case TYPES.SET_LESSONS:
			state.lessons.loading = false;
			state.lessons.hasMore = action.payload.hasMore;
			state.lessons.list = [...state.lessons.list, ...action.payload.list];
			state.lessons.offset = state.lessons.offset + action.payload.list.length;
			return state;
		case TYPES.REFRESH_LESSONS:
			state.lessons.hasMore = action.payload.hasMore;
			state.lessons.list = action.payload.list;
			state.lessons.offset = action.payload.list.length;
			return state;
		default:
			return state;
	}
}
